<?php

namespace App\Http\Controllers;

use App\Http\BaseInterface;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use phpDocumentor\Reflection\Types\Mixed_;


class TestController extends BaseController
{

    //继承一个抽象类 必须实现他里面的抽象方法  普通方法可以不实现
    //继承一个接口 必须实现他里面的所有方法
    //trait 用里面的方法就行  也可以重写哦
    /**
     * @param string $name
     * @param int $age
     * @return string
     */
//    function say(string $name = 'lmk', int $age = 20): string
//    {
//        //我要实现 抽象类中的say方法  而且参数要保持一致啊
//        return $name . $age;
//    }

    function test(Request $request, User $user)
    {
//        $users = DB::table('users')->select('name', 'email', 'id')->limit(3)->get()->toArray();


//        foreach ($users as $v) {
//            $user->setUser($v);
//        }

        dd($user->getuser());

        $collection = collect(['name', 'age', ''])->filter(function ($val) {
            if ($val == '') return true;
        })

//            ->combine(['lmk',29])
            ->all();

        $data = ['name', 'age'];
        $field = array_map(fn($val) => 'a.' . $val, array_filter($data));
//        $combined = $collection->combine(['George', 29]);

//        $combined->all();

// ['name' => 'George', 'age' => 29]
        dd($field);


        $collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);

        $collapsed = $collection->collapse();
        dd($collapsed);
        dd($collapsed->all());

// [1, 2, 3, 4, 5, 6, 7, 8, 9]

// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]
// [1, 2, 3, 4, 5, 6, 7, 8, 9]


        $validator = Validator::make($request->all(), [
            'file' => [
                'required',
                'max:255',
                function ($attribute, $value, $fail) {
//            dd($attribute);
                    if ($attribute === 'file') {
                        $fail($attribute . ' is invalid.');
                    }
                },
            ],
        ]);

        if ($validator->fails()) {
            dd($validator->errors());
            return redirect('post/create')
                ->withErrors($validator)
                ->withInput();
        }
        $s = $request->validate([
            'title' => 'required|unique:posts|max:255',
            'author.name' => 'required'
        ]);
        dd(1);
    }

    public function walk($a = 0, $b = 1)
    {
        // TODO: Implement walk() method.
    }

    public function eat()
    {
        // TODO: Implement eat() method.
    }
}
